﻿using Sunny.UI;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace DevelopTool.Common
{
    /// <summary>
    /// 时间帮助类
    /// </summary>
    public static class DateTimeHelper
    {

        /// <summary>
        /// 获取该时间相对于1970-01-01T00:00:00Z的天时间戳
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static double GetTotalDays(this in DateTime dt) => new DateTimeOffset(dt).Offset.TotalDays;


        /// <summary>
        /// 获取该时间相对于1970-01-01T00:00:00Z的小时时间戳
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static double GetTotalHours(this in DateTime dt) => new DateTimeOffset(dt).Offset.TotalHours;

        /// <summary>
        /// 获取该时间相对于1970-01-01T00:00:00Z的分钟时间戳
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static double GetTotalMinutes(this in DateTime dt) => new DateTimeOffset(dt).Offset.TotalMinutes;

        /// <summary>
        /// 获取该时间相对于1970-01-01T00:00:00Z的秒时间戳
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static long GetTotalSeconds(this in DateTime dt) => new DateTimeOffset(dt).UtcDateTime.Ticks / 10_000_000L - 62135596800L;

        /// <summary>
        /// 获取该时间相对于1970-01-01T00:00:00Z的毫秒时间戳
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static long GetTotalMilliseconds(this in DateTime dt) => new DateTimeOffset(dt).UtcDateTime.Ticks / 10000L - 62135596800000L;

        /// <summary>
        /// 获取该时间相对于1970-01-01T00:00:00Z的微秒时间戳
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static long GetTotalMicroseconds(this in DateTime dt) => (new DateTimeOffset(dt).UtcTicks - 621355968000000000) / 10;

        [DllImport("Kernel32.dll")]
        private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);

        /// <summary>
        /// 获取该时间相对于1970-01-01T00:00:00Z的纳秒时间戳
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static long GetTotalNanoseconds(this in DateTime dt)
        {
            var ticks = (new DateTimeOffset(dt).UtcTicks - 621355968000000000) * 100;
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                QueryPerformanceCounter(out var timestamp);
                return ticks + timestamp % 100;
            }

            return ticks + Stopwatch.GetTimestamp() % 100;
        }


        /// <summary>
        /// 时间戳转为C#格式时间
        /// </summary>
        /// <param name="TimeStamp"></param>
        /// <returns></returns>
        public static DateTime GetDateTime(string TimeStamp)
        {
            DateTime dtStart = TimeZoneInfo.ConvertTimeFromUtc(new DateTime(1970, 1, 1), TimeZoneInfo.Local);
            return TimeStamp.Length == 13 ? dtStart.AddMilliseconds(TimeStamp.ToLong()) : dtStart.AddSeconds(TimeStamp.ToLong());
        }

    }

    public static class TimeConverter
    {
        public static TimeSpan FromYears(double years) => TimeSpan.FromDays(years * 365); // 假设每年有365.25天（考虑闰年）  
        public static TimeSpan FromWeeks(double weeks) => TimeSpan.FromDays(weeks * 7);
        public static TimeSpan FromDays(double days) => TimeSpan.FromDays(days);
        public static TimeSpan FromHours(double hours) => TimeSpan.FromHours(hours);
        public static TimeSpan FromMinutes(double minutes) => TimeSpan.FromMinutes(minutes);
        public static TimeSpan FromSeconds(double seconds) => TimeSpan.FromSeconds(seconds);
        public static TimeSpan FromMilliseconds(double milliseconds) => TimeSpan.FromMilliseconds(milliseconds);
        public static TimeSpan FromMicroseconds(double microseconds) => TimeSpan.FromTicks((long)(microseconds * TimeSpan.TicksPerMillisecond / 1000));
        public static TimeSpan FromNanoseconds(double nanoseconds) => TimeSpan.FromTicks((long)(nanoseconds * TimeSpan.TicksPerMillisecond / 1_000_000));

        public static double ToYears(TimeSpan timeSpan) => timeSpan.TotalDays / 365; // 假设每年有365.25天（考虑闰年）  
        public static double ToWeeks(TimeSpan timeSpan) => timeSpan.TotalDays / 7;
        public static double ToDays(TimeSpan timeSpan) => timeSpan.TotalDays;
        public static double ToHours(TimeSpan timeSpan) => timeSpan.TotalHours; 
        public static double ToMinutes(TimeSpan timeSpan) => timeSpan.TotalMinutes;
        public static double ToSeconds(TimeSpan timeSpan) => timeSpan.TotalSeconds;
        public static double ToMilliseconds(TimeSpan timeSpan) => timeSpan.TotalMilliseconds;
        public static double ToMicroseconds(TimeSpan timeSpan) => (timeSpan.Ticks * 1000) / TimeSpan.TicksPerMillisecond;
        public static double ToNanoseconds(TimeSpan timeSpan) => (timeSpan.Ticks * 1_000_000) / TimeSpan.TicksPerMillisecond;
    }
}
